package com.hanyxx.thread.high;

/**
 * @author layman
 */
public class Producer extends Thread{
    private BaoZi baozi;
    public Producer(String name,BaoZi baozi){
        super(name);
        this.baozi = baozi;
    }

    @Override
    public void run() {
        int count = 0;
        while(true){
            synchronized (baozi){
                if(baozi.isFlag() == true){
                    try {
                        System.out.println("有包子，等待吃货吃包子");
                        System.out.println("--------------------");
                        baozi.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                // 没有包子，生产包子
                System.out.println("包子铺开始生产包子");
                if(count%2 == 0){
                    baozi.setXian("香菇鸡肉");
                }else{
                    baozi.setXian("韭菜鸡蛋");
                }
                count++;

                baozi.setFlag(true);
                System.out.println("包子生产好了："+baozi.getXian()+"包子");
                System.out.println("--------------------");
                try {
                    Thread.sleep(500L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                baozi.notify();
            }
        }
    }
}
